package com.lagou.edu.boss.rest.authroity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.lagou.edu.authority.ResourceRemoteService;
import com.lagou.edu.authority.dto.AllocateRoleResourceDTO;
import com.lagou.edu.authority.dto.ResourceCategoryDTO;
import com.lagou.edu.authority.dto.ResourceDTO;
import com.lagou.edu.authority.param.ResourceQueryParam;
import com.lagou.edu.boss.entity.form.AllocateRoleResourceForm;
import com.lagou.edu.boss.entity.form.ResourceCategoryForm;
import com.lagou.edu.boss.entity.form.ResourceForm;
import com.lagou.edu.common.entity.vo.Result;
import com.lagou.edu.common.util.ConvertUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * TODO
 *
 * @author lm
 * @since 2021/11/5 17:29
 */
@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    ResourceRemoteService resourceRemoteService;

    @ApiOperation(value = "按条件分页查询资源")
    @PostMapping("/getResourcePages")
    public Result<Page<ResourceDTO>> getResourcePages(@RequestBody ResourceQueryParam resourceQueryParam) {
        return Result.success(resourceRemoteService.getResourcePages(resourceQueryParam));
    }

    @ApiOperation(value = "查询资源分类列表", notes = "如果不传资源id值，则不标记是否选中该分类。传了id，会根据资源分类将分类列表中对应的分类标记为选中")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", name = "resourceId", value = "资源ID", required = false, paramType = "Integer")
    })
    @GetMapping("/category/getAll")
    public Result<List<ResourceCategoryDTO>> getCategories(@RequestParam(value = "resourceId", required = false) Integer resourceId) {
        List<ResourceCategoryDTO> resourceCategoryDTOList = resourceRemoteService.getAllCategories();
        if (CollectionUtils.isEmpty(resourceCategoryDTOList)) {
            return Result.success(Lists.newArrayList());
        }
        if (Objects.nonNull(resourceId)) {
            ResourceDTO resourceDTO = resourceRemoteService.getResourceDTOById(resourceId);
            if (Objects.nonNull(resourceDTO)) {
                resourceCategoryDTOList.stream().forEach(category -> {
                    if (Objects.equals(category.getId(), resourceDTO.getCategoryId())) {
                        category.setSelected(true);
                    }
                });
            }
        }
        return Result.success(resourceCategoryDTOList);
    }

    @ApiOperation(value = "保存或者更新资源")
    @PostMapping("/category/saveOrUpdate")
    public Result<Boolean> saveOrUpdateResourceCategory(@RequestBody ResourceCategoryForm resourceCategoryForm) {
        ResourceCategoryDTO resourceCategoryDTO = ConvertUtils.convert(resourceCategoryForm, ResourceCategoryDTO.class);
        if (Objects.isNull(resourceCategoryDTO.getId())) {
            //resourceDTO.setCreatedBy(UserManager.getUserName());
            resourceCategoryDTO.setCreatedBy("张三");
            resourceCategoryDTO.setCreatedTime(new Date());
        }
        // resourceDTO.setUpdatedBy(UserManager.getUserName());
        resourceCategoryDTO.setUpdatedBy("张三");
        resourceCategoryDTO.setUpdatedTime(new Date());
        return Result.success(resourceRemoteService.saveOrUpdateResourceCategory(resourceCategoryDTO));
    }

    @ApiOperation(value = "保存或者更新资源分类")
    @PostMapping("/saveOrUpdate")
    public Result<Boolean> saveOrUpdateResource(@RequestBody ResourceForm resourceForm) {
        ResourceDTO resourceDTO = ConvertUtils.convert(resourceForm, ResourceDTO.class);
        if (Objects.isNull(resourceDTO.getId())) {
            //resourceDTO.setCreatedBy(UserManager.getUserName());
            resourceDTO.setCreatedBy("张三");
            resourceDTO.setCreatedTime(new Date());
        }
        // resourceDTO.setUpdatedBy(UserManager.getUserName());
        resourceDTO.setUpdatedBy("张三");
        resourceDTO.setUpdatedTime(new Date());
        return Result.success(resourceRemoteService.saveOrUpdateResource(resourceDTO));
    }

    @ApiOperation(value = "获取所有资源")
    @GetMapping("/getAll")
    public Result<List<ResourceDTO>> getAll() {
        return Result.success(resourceRemoteService.getAllResource());
    }

    @ApiOperation(value = "给角色分配资源")
    @PostMapping("/allocateRoleResources")
    public Result<Boolean> allocateRoleResources(@RequestBody AllocateRoleResourceForm allocateRoleResourceForm) {
        log.info("Allocate role resources, params:{}", allocateRoleResourceForm);
        if (Objects.isNull(allocateRoleResourceForm.getRoleId())) {
            return Result.fail("角色ID不能为空");
        }
        AllocateRoleResourceDTO allocateRoleMenuDTO = ConvertUtils.convert(allocateRoleResourceForm, AllocateRoleResourceDTO.class);
        //allocateRoleMenuDTO.setCreatedBy(UserManager.getUserName());
        //allocateRoleMenuDTO.setUpdatedBy(UserManager.getUserName());
        allocateRoleMenuDTO.setCreatedBy("张三");
        allocateRoleMenuDTO.setUpdatedBy("张三");
        return Result.success(resourceRemoteService.allocateRoleResources(allocateRoleMenuDTO));
    }
}
